package com.bugbuff.dubbo.server.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bugbuff.dubbo.server.service.MessageService;
import com.bugbuff.dubbo.server.vo.MessagePageResult;
import com.bugbuff.dubbo.server.vo.MessageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("management")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 消息拒绝
     * @param list  视频或动态id集合
     * @return
     */
    @PostMapping("manage/messages/reject")
    public ResponseEntity<String> reject(@RequestBody List<String> list){

        /*System.out.println(list);
        return ResponseEntity.ok("lalala");*/

        try {
            String message = messageService.reject(list);
            if (StrUtil.isNotEmpty(message)){
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("消息拒绝失败，list="+list,e);
        }

        return ResponseEntity.status(500).body(null);

    }

    /**
     * 消息通过
     * @param list
     * @return
     */
    @PostMapping("/manage/messages/pass")
    public ResponseEntity<String> pass(@RequestBody List<String> list){

        /*System.out.println(list);
        return ResponseEntity.ok("lalala");*/

        try {
            String message = messageService.pass(list);
            if (StrUtil.isNotEmpty(message)){
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {

            log.error("消息通过失败，list="+list,e);
        }

        return ResponseEntity.status(500).body(null);

    }

    /**
     * 消息置顶
     * @param id
     * @return
     */
    @PostMapping("/manage/messages/{id}/top")
    public ResponseEntity<String> top(@PathVariable("id")String id){

        try {
            String messageMap = messageService.top(id);
            if (StrUtil.isNotEmpty(messageMap)){
                return ResponseEntity.ok(messageMap);
            }
        } catch (Exception e) {
            log.error("消息置顶失败,消息id="+id,e);
        }

        return ResponseEntity.status(500).body(null);
    }

    /**
     * 消息取消置顶
     * @param id
     * @return
     */
    @PostMapping("/manage/messages/{id}/untop")
    public ResponseEntity<String> unTop(@PathVariable("id")String id){

        try {
            String messageMap = messageService.unTop(id);
            if (StrUtil.isNotEmpty(messageMap)){
                return ResponseEntity.ok(messageMap);
            }
        } catch (Exception e) {
            log.error("消息置顶失败,消息id="+id,e);
        }

        return ResponseEntity.status(500).body(null);
    }


    /**
     * 消息详情
     * @return
     */
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity<MessageVo> getMessageByMessage(@PathVariable("publishId")String publishId){

        try {
            MessageVo messageVo = messageService.getPublishByPid(publishId);
            if (ObjectUtil.isNotEmpty(messageVo)){
                return ResponseEntity.ok(messageVo);
            }
        } catch (Exception e) {

            log.error("查询消息详情失败publishId="+publishId,e);
        }

        return ResponseEntity.status(500).body(null);
    }

    /**
     * 消息撤销
     * @return
     */
    @PostMapping("/manage/messages/revocation")
    public ResponseEntity<String> revocationMessage(@RequestBody List<String> list){

        try {
            String message = messageService.revocationMessage(list);
            if (StrUtil.isNotEmpty(message)){
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("消息撤销失败,", e);

        }

        return ResponseEntity.status(500).body(null);

    }

    /**
     * 消息翻页
     * @param pagesize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param id
     * @param sd
     * @param ed
     * @param uid
     * @param state
     * @return
     */
    @GetMapping("/manage/messages")
    public ResponseEntity<MessagePageResult> getMessagePage(@RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize,
                                                            @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                                                            @RequestParam(value = "sortProp",required = false)String sortProp,
                                                            @RequestParam(value = "sortOrder",required = false)String sortOrder,
                                                            @RequestParam(value = "id",required = false,defaultValue = "0")String id,
                                                            @RequestParam(value = "sd",required = false)Long sd,
                                                            @RequestParam(value = "ed",required = false)Long ed,
                                                            @RequestParam(value = "uid",required = false)Long uid,
                                                            @RequestParam(value = "state",required = false)Integer state){

        try {
            MessagePageResult messagePageResult = messageService.getMessagePage(page,pagesize,sortProp,sortOrder,id,sd,ed,uid,state);

            return ResponseEntity.ok(messagePageResult);

        } catch (Exception e) {

            log.error("消息翻页失败",e);
        }

        return ResponseEntity.status(500).body(null);
    }
}
